const listMixin = {
  data() {
    return {
      config: {
        pageCount: 0,
        currentPage: 1,
        pageSize: 20,
        total: 0
      }
    }
  },
  methods: {
    /**
     * 获取表格数据，限制为get请求，已处理异常，已触发loading事件
     * @param url url
     * @param option 直接传params对象
     * @returns {Promise<*>} 获取到的数据
     */
    mGetTableData(url, option) {
      let { currentPage, pageSize } = this.config
      let params = {
        pageIndex: currentPage,
        pageSize,
        ...option
      }
      return this.mGet(url, params).then((res) => {
        let { pageCount, rowCount, pageSize } = res
        this.config.pageCount = pageCount
        this.config.total = rowCount
        this.config.pageSize = pageSize
        return Promise.resolve(res)
      })
    },

    /**
     * 封装get请求
     * @param url url
     * @param params 其他参数
     * @param needLoading 是否需要loading
     * @returns {Promise<any>} resolve 服务器返回的数据
     */
    mGet(url, params = {}, needLoading = true) {
      if (needLoading) {
        this.$emit('loading', true)
      }
      return new Promise((resolve, reject) => {
        this.$http
          .get(url, { params })
          .then((res) => {
            if (res.body.errMessage) {
              setTimeout(() => {
                this.$message.error(res.body.errMessage)
              }, 0)
              // eslint-disable-next-line prefer-promise-reject-errors
              return reject()
            }
            resolve(res.body.data)
          })
          .catch((e) => {
            // eslint-disable-next-line prefer-promise-reject-errors
            reject()
          })
      })
    },

    /**
     * 检查表单，如果不是有ref的a-form表单则不检查
     * @param formName 表单名称，默认为mainForm
     * @returns {Promise<*>}
     */
    mCheckForm(formName = 'mainForm') {
      return new Promise((resolve, reject) => {
        let form = this.$refs[formName]
        if (form instanceof Array) {
          form = form[0] || undefined
        }
        if (!form || !form.validate) {
          // 没有定义ref或者不是a-form，不需要检查
          return resolve()
        }
        form.validate((v) => {
          if (!v) {
            this.$message.error('请按照要求填写表单')
            // eslint-disable-next-line prefer-promise-reject-errors
            reject('processed')
          } else {
            resolve()
          }
        })
      })
    },

    /**
     * 清除表单验证
     * @param formName 默认为mainForm
     */
    mClearValidate(formName = 'mainForm') {
      let form = this.$refs[formName]
      if (form && form.clearValidate) {
        this.$nextTick(() => {
          form.clearValidate()
        })
      }
    },
    /**
     * $msgbox的封装，并添加异常处理
     * @param el 试用$createEl创建的元素
     * @param title 标题默认为：温馨提示
     * @returns {Promise<*>} resolve为点击确定后的处理
     */
    mMsg(el, title = '温馨提示') {
      return new Promise((resolve, reject) => {
        this.$confirm({
          title,
          content: el,
          okText: '确认',
          cancelText: '取消',
          icon: 'info-circle',
          centered: true,
          onOk() {
            resolve()
          }
        })
      })
    },

    /**
     * 提交表单数据，限制为post请求，处理异常和loading事件
     * @param url url
     * @param body 请求体
     * @param option 其他request选项
     * @returns {Promise<*>} resolve返回的数据
     */
    mPost(url, body = {}, option = {}) {
      return new Promise((resolve, reject) => {
        this.$emit('loading', true)
        this.$http
          .post(url, body, option)
          .then((res) => {
            this.$emit('loading')
            if (!res.body) {
              // eslint-disable-next-line prefer-promise-reject-errors
              return reject('processed')
            }
            if (res.body.errMessage) {
              setTimeout(() => {
                this.$message.error(res.body.errMessage)
              }, 0)
              // this.$nextTick(() => {
              //   this.$message.error(res.body.errMessage)
              // })
              // eslint-disable-next-line prefer-promise-reject-errors
              return reject('processed')
            }
            resolve(res.body.data)
          })
          .catch((e) => {
            this.$emit('loading')
            // eslint-disable-next-line prefer-promise-reject-errors
            reject('processed')
          })
      })
    }
  }
}
export { listMixin }
